<?php 

include("helper/DBOperation.php");
$dbObj = new DBOperation();

include("helper/check_user_login.php");
checkUserIsLogin();

if(isset($cookie['userslog']['id']) && $cookie['userslog']['id']!=""){
	$userId = $cookie['userslog']['id'];
	$get_user = mysql_fetch_array(mysql_query("select * from tbl_users where id = '".$userId."'"));	
}

if($_POST){
	$username  = $_POST['username'];
	$password  = $_POST['password'];
	$cpassword = $_POST['cpassword'];
	
	$error ="";
	$success ="";

	if($username==""){
		$error.= "Username can not be left blank. <BR>";
	}

	if(strstr($username," ")){
		$error.= "Username can not contain space. <BR>";
	}	
	
	if($cpassword!=$password){
		$error.= "Password and Confirm Password does not match. <BR>";
	}	

	if($cookie['userslog']['username']!=$username){
		$username  = mysql_real_escape_string($username);
		$chk_uname = mysql_num_rows(mysql_query("select * from tbl_users where username = '".$username."'"));
		if($chk_uname>0){
			$error.= "This Username already exist. <BR>";
		}
	}

	if($error==""){	

		$id = $cookie['userslog']['id'];				
		
		$query = "Update tbl_users set username = '".mysql_real_escape_string($username)."' ";
		
		if($password!=""){			
			$query.= " , password='".md5(mysql_real_escape_string($password))."'";
		}
		
		$query.= " where id='".$id."'";
		

		if(mysql_query($query)){
			$cookie['userslog']['username'] = $username;
			if($password!=""){
				$cookie['userslog']['password'] = md5($password);
			}
			
			$success = "Information updated successfully.";			
		}else{
			$error = "Information can not be updated.";
		}		
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta name = "viewport" content = "width = device-width">

<meta name="apple-mobile-web-app-capable" content="yes" />
  <title>User Preference</title>
<link href="HBMS_css.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="iui.css" type="text/css" />

  <link rel="stylesheet" title="Default" href="default-theme.css"  type="text/css"/> 
 
   <script type="application/x-javascript" src="js/iui.js"></script>

  <script type="text/javascript">
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){

// If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
var noddy, remotes = false;

document.addEventListener('click', function(event) {

noddy = event.target;

// Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
noddy = noddy.parentNode;
}

if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes))
{
event.preventDefault();
document.location.href = noddy.href;
}

},false);
}

</script>
<script language="JavaScript" src="js/jquery-1.5.2.min.js"></script>
<script language="JavaScript" src="js/jquery.validate.js"></script>
<script type="text/javascript">

	function GetXmlHttpObject(){
		var xmlHttp=null;
		try
  			{
  				// Firefox, Opera 8.0+, Safari
  				xmlHttp=new XMLHttpRequest();
  			}
		catch (e)
  			{
  				// Internet Explorer
  				try
    				{
    					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    				}
  				catch (e)
    				{
    					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    				}
  			}
		return xmlHttp;
		}
	
	function notification(id)
     {
	
	    xmlHttp = GetXmlHttpObject();
	    if(xmlHttp==null)
	     {
		    alert("Your browser does not support AJAX!");
		    return;
	     }	
		
		var status = document.getElementById(id).getAttribute("toggled");		
  			
	    var url = "notification_save.php?notification_type="+id+"&status="+status;
	    xmlHttp.onreadystatechange=function()
	     {
		
		    if(xmlHttp.readyState==4)
		     {
			   var response = xmlHttp.responseText;					
		     }
	     }
	   xmlHttp.open('GET',url,true);
	   xmlHttp.send(null);
     
	 }


</script>  
</head>

<body>

  
    
   <div class="panel" >
   <div class="unit_a_top_row">
        	<div class="unit_a_cemusa_img"></div>
		<?php if($cookie['userslog']['is_admin']==1){ ?>
				<a href="admin/user_listing.php" style="text-decoration:none;color:white;"><img src="images/person.png" width="20px" height="20px" title="Add User"/></a>
			<?php }?>
            <div class="unita_top_text_part">
            	<div class="unita_top_txt1"><span>logged in as:</span> <?=$cookie['userslog']['display_name']?></div>
                <div class="unita_top_txt1"><span>last update:</span> 
		<?php if($lastLogUpdateTime = $dbObj->lastUpdatedLogTime()) { echo $lastLogUpdateTime; } else { echo "00:00:00"; } ?></div>
            </div>
        </div>
        <div class="prefs_middle_content">
        <div class="panel">

        <h2>Notifications</h2>
		<?php	$text_toggle = "";$email_toggle = ""; 
				if($get_user['text_alert']==1){
					$text_toggle = "toggled='true'";					
				}
				if($get_user['email_alert']==1){
					$email_toggle = "toggled='true'";					
				}
?>
        <fieldset>
            <div class="row">
                <label>Text Alert</label>
                <div id="text" class="toggle" onclick="notification('text');" <?php echo $text_toggle;?>>
					<span class="thumb"></span>
					<span class="toggleOn">ON</span>
					<span class="toggleOff">OFF</span>
				</div>
            </div>
            <div class="row">
                <label>Email Alert</label>
                <div id="email" class="toggle" onclick="notification('email');" <?php echo $email_toggle;?>>
					<span class="thumb"></span>
					<span class="toggleOn">ON</span>
					<span class="toggleOff">OFF</span>
				</div>
            </div>
        </fieldset>
        
        <h2>User</h2>
		<form name="webform" id="webform" action="user_preference.php" method="post">
        <fieldset>
			<div class="error" ><?php if(isset($error) && $error!="") { echo $error; }?></div>
			<div class="success" ><?php if(isset($success) && $success!="") { echo $success; }?></div>
            <div class="row">

                <label>Name</label>
                <input type="text" id="username" name="username" value="<?=$cookie['userslog']['username']?>" />
            </div>
            <div class="row">
                <label>Password</label>
                <input type="password" id="password" name="password" value="" />
            </div>
            <div class="row">

                <label>Confirm</label>
                <input type="password" id="cpassword" name="cpassword" value=""/>
            </div>
			 <div class="row">

                <input id="submit" type="submit" name="submit" value="Submit" style="cursor:pointer"/>
            </div>
        </fieldset>
		</form>
        </div>
        </div>
         <div class="unita_showtell_row">
        	<div class="unita_showtell_img"></div>

        </div>
        <div class="unita_bottm_menu">
        	<?php include("footer.php");?>
            </div>

        </div>
</div>
      
        

</body>
<script>
	$(document).ready(function(){	
		jQuery.validator.addMethod("noSpace", function(value, element) { 
  return value.indexOf(" ") < 0 && value != ""; 
}, "No space please allowed.");	

		$("#webform").validate({
			  rules: {
				'cpassword': {
				  equalTo: "#password"
				 },
      				'username': {
          			   noSpace: true
      				}
			  }
		});
	});

	

</script>
</html>

